package com.dating.libs.client.parttern.asa;

import java.io.IOException;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public abstract class ADbManager {
	public static final String TAG="Class: aDbManager";
	
	public static final String _id="_id";
	
	Context context;
	public ADbSqlHelper help;
	
	public String TABLE = null;
	public String[] COLUMNS=null;
	
	public ADbObject object=null;
	public ArrayList<ADbObject> array=null;
	
	/**
	 * constructor Database Manager
	 * @see init(Context, DataBaseHelper) must be called  after constructor
	 * */
	public ADbManager(Context context,String TABLE,String[] COLUMNS) {
		this.COLUMNS=COLUMNS;
		this.context=context;
		this.TABLE=TABLE;
	}
	
	/**
	 * initial SqlHelper and Context. Must be called when constructor child class of aDbManager called
	 * */
	public void init(Context context,ADbSqlHelper help) {
		if(help!=null)
		try {
			help.createDataBase();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @see check initial for TABLE and COLUMNS
	 * @return true: success or false: failed
	 * */
	private boolean checkInit(){
		if(null==TABLE || TABLE.equals("")){
			try {
				throw new Throwable("not init TABLE");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return false;
		}
		if(null==COLUMNS || COLUMNS.equals("")){
			try {
				throw new Throwable("not init COLUMN");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return false;
		}else
			return true;
	}

	/**
	 * @return select * from test
	 * */
	public abstract ArrayList<?> select() throws SQLiteException;

	/**
	 * @return aObject class
	 * @see should override
	 * */
	public abstract ADbObject select(int id) throws SQLiteException ;
	
	/**
	 *  WHERE field=value CLAUSE
	 * @return aObject class
	 * @see should override
	 * */
	public abstract ArrayList<?> select(String field,String value) throws SQLiteException ;

	/**
	 * @return size records of table
	 * */
	public int getLastIndex()  throws SQLiteException{
		if(!checkInit())
			return -1;
		int id = -1;
		SQLiteDatabase db = help.getReadableDatabase();
		Cursor cursor = db.query(TABLE, new String[] { _id }, null,
				null, null, null, _id);
		cursor.moveToFirst();
		if(cursor.getCount()==0){
			return 0;
		}
		do {
			id = cursor.getInt(cursor.getColumnIndex(_id));
		} while (cursor.moveToNext());
		cursor.close();
		return id;
	}

	/**
	 * @return rowID for insert. -1: error
	 * */
	public long insertTo(int id, String... values) throws SQLException {
		if(!checkInit())
			return -1;
		ContentValues content = new ContentValues();
		content.put("_id", id);
		for (int i = 0; i < values.length; ++i) {
			content.put(COLUMNS[i + 1], values[i]);
		}
		SQLiteDatabase db = help.getWritableDatabase();
		return db.insertOrThrow(TABLE, null, content);
	}
	/**
	 * update from table
	 * */
	public long updateTo(String table,int id, String... values) throws SQLException {
		if(!checkInit())
			return -1;
		ContentValues content = new ContentValues();
		content.put("_id", id);
		for (int i = 0; i < values.length; ++i) {
			content.put(COLUMNS[i + 1], values[i]);
		}
		SQLiteDatabase db = help.getWritableDatabase();
		long value = db.update(table, content, "_id='"+id+"'", null);
		db.close();
		return value;
	}
	/**
	 * @return rowID for insert new record. -1: error
	 * */
	public long insertNewTo(String... values) throws SQLException {
		if(!checkInit())
			return -1;
		int id = getLastIndex() + 1;
		return insertTo(id, values);
	}

	/**
	 * @return number of record updated
	 * */
	public int edit(int id, String... values) throws SQLiteException {
		if(!checkInit())
			return -1;
		ContentValues content = new ContentValues();
		content.put("_id", id);
		for (int i = 0; i < values.length; ++i) {
			content.put(COLUMNS[i + 1], values[i]);
		}
		SQLiteDatabase db = help.getWritableDatabase();
		return db.update(TABLE, content, _id + "='" + id + "'", null);
	}

	/**
	 * @see reindex() must be called after delete()
	 * @return number record deleted
	 * */
	public boolean delete(int id) throws SQLiteException {
		if(!checkInit())
			return false;
		SQLiteDatabase db = help.getWritableDatabase();
		int sum = db.delete(TABLE, _id + "='" + id + "'", null);
		if (sum > 0)
			return true;
		else
			return false;
	}
	
	/**
	 * @return number record re-index
	 * */
	public int reindex() throws SQLiteException{
		if(!checkInit())
			return -1;
		int id = 0;
		SQLiteDatabase db = help.getReadableDatabase();
		Cursor cursor = db.query(TABLE, COLUMNS, null, null, null, null,
				_id);
		cursor.moveToFirst();
		do {
			int _index = cursor.getInt(cursor.getColumnIndex(_id));
			int size = COLUMNS.length - 1;
			String[] values = new String[size];
			for (int i = 0; i < size; ++i) {
				values[i] = cursor.getString(i + 1);
			}
			if (id == _index) {
				edit(_index, values);
			} else {
				delete(_index);
				insertTo(id, values);
			}
			id++;
		} while (cursor.moveToNext());
		cursor.close();
		return id;
	}
	
}
